/** 文件作用：主活动
 *	创建时间：2012-11-17 下午7:49:28
 *  作者： 陈相伯 
 *  描述：
 */
package com.kingPadStudy.activity; 
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipException;

import com.kingPadStudy.constant.Constant;
import com.kingPadStudy.controller.ViewController;
import com.kingPadStudy.data.BookData;
import com.kingPadStudy.data.MetaData;
import com.kingPadStudy.resource.ResourceLoader;
import com.kingPadStudy.resource.ResourcePacker;
import com.kingPadStudy.security.Security;
import com.kingPadStudy.tools.EuipmentInfoCatcher;
import com.kingPadStudy.tools.LoadData;
import com.kingPadStudy.tools.RequestListener;
import com.kingPadStudy.tools.RequestParameter;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * 描述：主活动
 *
 */
public class KingPadStudyActivity extends Activity {
	private long mExitTime ;
	
	private static ProgressDialog progressDialog;
	private static Context context;
	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        //初始化
//        init();
//		//默认加载语文人教版 2A
//        ResourceLoader.loadMetaData("/mnt/sdcard/kingpad/data/Chinese/package/必背古诗/二级/学习/黄鹤楼送孟浩然之广陵/古诗精讲","Flash",null);
        //TODO 测试设备信息
//        System.out.println("信息是："+EuipmentInfoCatcher.getEuipmentInfoCatcher(this).toString());
        //TODO  主界面测试
        //设置屏幕属性
  		setScreenAttribute();
  		setScreenScale();
  		//初始化视图控制器
  		//进入主界面
  		ViewController.intiView(this, this);
  		context = this;
		ViewController.controllView(Constant.VIEW_MAIN);
    }
    
	/** 作用：初始化
	 * 时间：20122012-11-17下午10:08:37
	 * void
	 */
	private void init() {
		//设置屏幕属性
		setScreenAttribute();
		setScreenScale();
		//初始化视图控制器
		ViewController.intiView(this, this);
		//检测是否激活
		if(Security.isActivated(this)){
			//当已经激活时，设置登陆视图
			ViewController.controllView(Constant.VIEW_LOGIN);
		}else{
			//当还未激活时，设置的激活视图
			ViewController.controllView(Constant.VIEW_ACTIVATE);
		}
//		//默认加载语文人教版 2A
//        ResourceLoader.loadBigBookData("/mnt/sdcard/kingpad/data/Chinese/人教版_2a");
	}
	
	 public static Handler handler =  new Handler(){
	    	public void handleMessage(Message msg){
	    		ViewController.setMajor(msg.arg1);
	    		ViewController.controllView(msg.what);
	    	}
	    };
	
	/**
	 *	函数作用：设置屏幕比例
	 * void
	 */
	private void setScreenScale(){
		//初始化屏幕适配
        DisplayMetrics dm = new DisplayMetrics();   
        getWindowManager().getDefaultDisplay().getMetrics(dm);  
        Constant.setWidthScreen(dm.widthPixels); 
        Constant.setHeightScreen(dm.heightPixels); 
        Constant.setCompareIndex(); 
	}
	
	/** 作用：设置屏幕属性
	 * 时间：20122012-11-18下午2:41:21
	 * void
	 */
	private void setScreenAttribute() {
		//设置全屏
        setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏 
        //设置横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
	}
	
	public static void showWaitDialog(){
		dismissWaitDialog();
		progressDialog = ProgressDialog.show(context, "", "数据加载中...");
	}
	
	public static void dismissWaitDialog(){
		if (null != progressDialog && progressDialog.isShowing()){
			progressDialog.dismiss();
		}
	}
	
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {			
		if (keyCode == KeyEvent.KEYCODE_BACK) {
	        if ((System.currentTimeMillis() - mExitTime) > 2000) {
	                Object mHelperUtils;							
	                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
	                mExitTime = System.currentTimeMillis();			
	        } else {													
	    		ViewController.stopView();												
	            finish();
	    		System.exit(0);
	        }
	        return true;
	    }
        return super.onKeyDown(keyCode, event);
    }
    
}